package com.xujh;


//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Globally shared Jackson Tools
 *
 * @since 1.0
 *
 */
public final class JacksonFactory {

	private static ObjectMapper defaultObjectMapper = null;

	private static ObjectMapper polymorphicObjectMapper = null;

	/**
	 * 不支持多态性的ObjectMapper。
	 *
	 * default to using DefaultTyping.OBJECT_AND_NON_CONCRETE.
	 *
	 * @return ObjectMapper
	 */
	public static ObjectMapper getDefaultObjectMapper() {

		if (defaultObjectMapper == null)
			defaultObjectMapper = new ObjectMapper();

		return defaultObjectMapper;
	}

	/**
	 * 支持多态性的ObjectMapper
	 *
	 * using DefaultTyping.NON_FINAL
	 *
	 * @return
	 */
	public static ObjectMapper getPolymorphicObjectMapper() {

		if (polymorphicObjectMapper == null) {
			polymorphicObjectMapper = new ObjectMapper()
					.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
			//JsonSerialize.Inclusion
			polymorphicObjectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		}

		return polymorphicObjectMapper;
	}

	public static void configureFailOnUnknowProp() {
		//DeserializationConfig.Feature
		configureFailOnUnknowProp(getPolymorphicObjectMapper());
	}

	public static void configureFailOnUnknowProp(ObjectMapper polymorphicObjectMapper) {
		//DeserializationConfig.Feature
		polymorphicObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}

//	public static <T> T readValue(String value, Class<T> cls) throws IOException {
//		ObjectMapper polymorphicObjectMapper = JacksonFactory.getPolymorphicObjectMapper();
//		configureFailOnUnknowProp(polymorphicObjectMapper);
//		return polymorphicObjectMapper.readValue(value, cls);
//	}
}
